/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my12306;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import my12306.bean.QueryResult;
import my12306.http.Consts;
import my12306.http.TrainInfo;
import org.jdesktop.application.Action;

/**
 *
 * @author Jack
 */
public class TrainSelectDialog extends javax.swing.JDialog {

    private HashMap<String, TrainInfo> allTrain;
    private TrainInfoPanel traininfoPanel;
    private My12306ViewMini view;
    private String from;
    private String to;
    private String startDate;
    private String startTime;

    /**
     * Creates new form TrainSelectDialog
     */
    public TrainSelectDialog(My12306ViewMini view) {
        initComponents();
        setModal(true);
        this.view = view;
//        jSplitPane1.setRightComponent(getTrainInfoPanel());
        jScrollPane1.setViewportView(getTrainInfoPanel());
    }

    public String getSelectTrainClass() {
        StringBuilder sb = new StringBuilder("");
        if (jCheckBoxQB.isSelected()) {
            sb.append(java.util.ResourceBundle.getBundle("my12306/resources/TrainSelectDialog").getString("QB#"));
        }
        if (jCheckBoxD.isSelected()) {
            sb.append(java.util.ResourceBundle.getBundle("my12306/resources/TrainSelectDialog").getString("D#"));
        }
        if (jCheckBoxZ.isSelected()) {
            sb.append(java.util.ResourceBundle.getBundle("my12306/resources/TrainSelectDialog").getString("Z#"));
        }
        if (jCheckBoxT.isSelected()) {
            sb.append(java.util.ResourceBundle.getBundle("my12306/resources/TrainSelectDialog").getString("T#"));
        }
        if (jCheckBoxK.isSelected()) {
            sb.append(java.util.ResourceBundle.getBundle("my12306/resources/TrainSelectDialog").getString("K#"));
        }
        if (jCheckBoxQT.isSelected()) {
            sb.append(java.util.ResourceBundle.getBundle("my12306/resources/TrainSelectDialog").getString("QT#"));
        }
        return sb.toString();
    }

    private void setSelection(boolean select) {
        jCheckBoxD.setSelected(select);
        jCheckBoxK.setSelected(select);
        jCheckBoxQT.setSelected(select);
        jCheckBoxT.setSelected(select);
        jCheckBoxZ.setSelected(select);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jCheckBoxQT = new javax.swing.JCheckBox();
        jCheckBoxD = new javax.swing.JCheckBox();
        jCheckBoxQB = new javax.swing.JCheckBox();
        btnQuery = new javax.swing.JButton();
        jCheckBoxK = new javax.swing.JCheckBox();
        jCheckBoxT = new javax.swing.JCheckBox();
        jCheckBoxZ = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(my12306.My12306App.class).getContext().getResourceMap(TrainSelectDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setName("jSplitPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(my12306.My12306App.class).getContext().getActionMap(TrainSelectDialog.class, this);
        jCheckBoxQT.setAction(actionMap.get("qtAction")); // NOI18N
        jCheckBoxQT.setSelected(true);
        jCheckBoxQT.setText(resourceMap.getString("jCheckBoxQT.text")); // NOI18N
        jCheckBoxQT.setName("jCheckBoxQT"); // NOI18N

        jCheckBoxD.setAction(actionMap.get("dAction")); // NOI18N
        jCheckBoxD.setSelected(true);
        jCheckBoxD.setText(resourceMap.getString("jCheckBoxD.text")); // NOI18N
        jCheckBoxD.setName("jCheckBoxD"); // NOI18N

        jCheckBoxQB.setAction(actionMap.get("qbAction")); // NOI18N
        jCheckBoxQB.setSelected(true);
        jCheckBoxQB.setText(resourceMap.getString("jCheckBoxQB.text")); // NOI18N
        jCheckBoxQB.setName("jCheckBoxQB"); // NOI18N

        btnQuery.setAction(actionMap.get("queryAction")); // NOI18N
        btnQuery.setText(resourceMap.getString("btnQuery.text")); // NOI18N
        btnQuery.setName("btnQuery"); // NOI18N

        jCheckBoxK.setAction(actionMap.get("kAction")); // NOI18N
        jCheckBoxK.setSelected(true);
        jCheckBoxK.setText(resourceMap.getString("jCheckBoxK.text")); // NOI18N
        jCheckBoxK.setName("jCheckBoxK"); // NOI18N

        jCheckBoxT.setAction(actionMap.get("tAction")); // NOI18N
        jCheckBoxT.setSelected(true);
        jCheckBoxT.setText(resourceMap.getString("jCheckBoxT.text")); // NOI18N
        jCheckBoxT.setName("jCheckBoxT"); // NOI18N

        jCheckBoxZ.setAction(actionMap.get("zAction")); // NOI18N
        jCheckBoxZ.setSelected(true);
        jCheckBoxZ.setText(resourceMap.getString("jCheckBoxZ.text")); // NOI18N
        jCheckBoxZ.setName("jCheckBoxZ"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(jCheckBoxQB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBoxD, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxZ)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBoxT)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxK)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxQT)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnQuery, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(181, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBoxQB)
                    .addComponent(jCheckBoxD)
                    .addComponent(jCheckBoxT)
                    .addComponent(jCheckBoxK)
                    .addComponent(jCheckBoxZ, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jCheckBoxQT)
                    .addComponent(btnQuery))
                .addContainerGap())
        );

        jSplitPane1.setTopComponent(jPanel1);

        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jSplitPane1.setRightComponent(jScrollPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
        );

        getAccessibleContext().setAccessibleParent(null);
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void queryAction() {
        new Thread() {
            @Override
            public void run() {
                log(java.util.ResourceBundle.getBundle("my12306/resources/TrainSelectDialog").getString("正在查询数据..."));
                QueryResult qr;
//                qr = trainClient.querySingleAction_init();
//
//                token_querySingleAction = trainClient.getTokensFromHtml(qr.getRawData()).getToken();

                allTrain = My12306App.getTrainClient().querySingleAction_queryLeftTicket(
                        getFrom(), getTo(), getStartDate(), getStartTime(), "",
                        getSelectTrainClass()).getTrains();
                List<List<Object>> data = new ArrayList<List<Object>>();
                for (TrainInfo item : getAllTrain().values()) {
                    data.add(item.toList());
                }

                getTrainInfoPanel().setRowData(data);
                log(String.format(java.util.ResourceBundle.getBundle("my12306/resources/TrainSelectDialog").getString("找到%D班列车。"), data.size()));
            }
        }.start();
    }

    @Action
    public void qbAction() {
        setSelection(jCheckBoxQB.isSelected());
    }

    @Action
    public void dAction() {
        jCheckBoxQB.setSelected(false);
    }

    @Action
    public void zAction() {
        jCheckBoxQB.setSelected(false);
    }

    @Action
    public void tAction() {
        jCheckBoxQB.setSelected(false);
    }

    @Action
    public void kAction() {
        jCheckBoxQB.setSelected(false);
    }

    @Action
    public void qtAction() {
        jCheckBoxQB.setSelected(false);
    }

    public List<TrainInfo> showDialog() {
        queryAction();
        setSize(new Dimension(800, 600));
        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((size.width - getSize().width) / 2, (size.height - getSize().height) / 2);
        setVisible(true);
        return getSelectedTrains();
    }

    /**
     * @return the traininfoPanel
     */
    public final TrainInfoPanel getTrainInfoPanel() {
        if (traininfoPanel == null) {
            traininfoPanel = new TrainInfoPanel(new ArrayList<List<Object>>(), Consts.columns);
        }
        return traininfoPanel;
    }

    /**
     * @param traininfoPanel the traininfoPanel to set
     */
    public void setTraininfoPanel(TrainInfoPanel traininfoPanel) {
        this.traininfoPanel = traininfoPanel;
    }

    /**
     * @return the from
     */
    public String getFrom() {
        return from;
    }

    /**
     * @param from the from to set
     */
    public void setFrom(String from) {
        this.from = from;
    }

    /**
     * @return the to
     */
    public String getTo() {
        return to;
    }

    /**
     * @param to the to to set
     */
    public void setTo(String to) {
        this.to = to;
    }

    /**
     * @return the startDate
     */
    public String getStartDate() {
        return startDate;
    }

    /**
     * @param startDate the startDate to set
     */
    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    /**
     * @return the startTime
     */
    public String getStartTime() {
        return startTime;
    }

    /**
     * @param startTime the startTime to set
     */
    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    /**
     * @return the allTrain
     */
    private HashMap<String, TrainInfo> getAllTrain() {
        if (allTrain == null) {
            allTrain = new HashMap<String, TrainInfo>();
        }
        return allTrain;
    }

    private List<TrainInfo> getSelectedTrains() {
        List<TrainInfo> result = new ArrayList<TrainInfo>();
        try {
            List<List<Object>> rows = getTrainInfoPanel().getSelectedRows();
            if (rows == null || rows.isEmpty()) {
                rows = getTrainInfoPanel().model.getRowData();
            }
            for (List<Object> row : rows) {
                String trainno = row.get(0).toString();
                result.add(allTrain.get(trainno));
            }
        } catch (Exception ex) {
//            log.error(ex.getLocalizedMessage(), ex);
        }
        return result;
    }

    void log(String msg) {
        view.log(msg);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnQuery;
    private javax.swing.JCheckBox jCheckBoxD;
    private javax.swing.JCheckBox jCheckBoxK;
    private javax.swing.JCheckBox jCheckBoxQB;
    private javax.swing.JCheckBox jCheckBoxQT;
    private javax.swing.JCheckBox jCheckBoxT;
    private javax.swing.JCheckBox jCheckBoxZ;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    // End of variables declaration//GEN-END:variables
}
